package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.redis.CacheHelper;
import com.atguigu.gmall.activity.service.ActivityService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.activity.UserRecode;
import com.atguigu.gmall.rebbit.constant.MqConst;
import com.atguigu.gmall.rebbit.constant.RedisConst;
import com.atguigu.gmall.rebbit.service.RabbitService;
import org.apache.ibatis.executor.ResultExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/api/activity/seckill")
public class SeckillApiController {
    public static final String salt = "qazxsw";
    @Autowired
    private ActivityService activityService;
    @Autowired
    private RabbitService rabbitService;
    @Autowired
    private RedisTemplate redisTemplate;
    //获取抢购码
    @GetMapping("/auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable Long skuId, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        SeckillGoods seckillGoods = activityService.findBySkuId(skuId);
        //1:活动时间
        Date startTime = seckillGoods.getStartTime();
        Date endTime = seckillGoods.getEndTime();
        Date curTime = Calendar.getInstance().getTime();
        if (DateUtil.dateCompare(startTime, curTime) && DateUtil.dateCompare(curTime, endTime)) {
            String encrypt = MD5.encrypt(salt + userId);
            return Result.ok(encrypt);
        } else {
            return Result.fail().message("不在活动时间内");
        }

    }

    //开始抢购
    @PostMapping("/auth/seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable Long skuId, String skuIdStr, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        String encrypt = MD5.encrypt(salt + userId);
        if (!encrypt.equals(skuIdStr)) {
            return Result.fail().message("抢购码失效");
        }
        //校验状态码
        Object status = CacheHelper.get(skuId.toString());
        if (!"1".equals(status)) {
            return Result.build(null, ResultCodeEnum.SECKILL_FINISH);
        }

        UserRecode userRecode = new UserRecode();
        userRecode.setUserId(userId);
        userRecode.setSkuId(skuId);
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_SECKILL_USER,
                MqConst.ROUTING_SECKILL_USER, userRecode);
        return Result.ok();
    }

    @GetMapping("/auth/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable Long skuId,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        //检查是否已购买商品
        Object o = redisTemplate.opsForValue().get(RedisConst.SECKILL_ORDERS_USERS + userId);
        if(o!=null){
            return Result.build(null,ResultCodeEnum.SECKILL_ORDER_SUCCESS);
        }
        Object o1 = redisTemplate.opsForValue().get(RedisConst.SECKILL_ORDERS + userId);
        if(o1!=null){
            return Result.build(null, ResultCodeEnum.SECKILL_SUCCESS);
        }
        Object o2 = redisTemplate.opsForValue().get(RedisConst.SECKILL_USER + userId);
        if(null!=o2){
            Object o3 = CacheHelper.get(skuId.toString());
            if("1".equals(o3)){
               return Result.build(null,ResultCodeEnum.SECKILL_RUN);
            }
        }

        return Result.build(null,ResultCodeEnum.SECKILL_FAIL);
    }
}
